package com.csun.cmny.provider.manager;

import com.csun.cmny.provider.model.constant.LongRangeServiceOrderStatusConstant;
import com.csun.cmny.provider.model.constant.LongRangeServiceOrderTypeConstant;
import com.csun.cmny.provider.model.domain.OmcLongRangeServiceOrder;
import com.csun.cmny.provider.service.OmcLongRangeServiceOrderService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class LongRangeServiceOrderManager {

    @Resource
    private OmcLongRangeServiceOrderService longRangeServiceOrderService;

    //定时让远程视频订单过期
    @Scheduled(fixedDelay = 1000*60*60)
    public void selectUserNameByUserId() {

        System.out.println("启动定时任务");
        List<OmcLongRangeServiceOrder> omcLongRangeServiceOrders = longRangeServiceOrderService.selectAll();

        for (OmcLongRangeServiceOrder omcLongRangeServiceOrder : omcLongRangeServiceOrders) {
            if (omcLongRangeServiceOrder.getOrderType()!=LongRangeServiceOrderTypeConstant.CONSULT){
                //如果没有次数了,写入没有次数
                if (omcLongRangeServiceOrder.getResidueNumber()<1){
                    omcLongRangeServiceOrder.setOrderStatus(LongRangeServiceOrderStatusConstant.NUMBEROFCOMPLETE);
                    longRangeServiceOrderService.update(omcLongRangeServiceOrder);
                }
                //如果是有效状态,计算是否过期
                if (omcLongRangeServiceOrder.getOrderStatus()==LongRangeServiceOrderStatusConstant.ALREADYIDENTIFIED){

                    //到期时间
                    Calendar enterTime = Calendar.getInstance();
                    enterTime.setTime(omcLongRangeServiceOrder.getEnretTime());
                    enterTime.add(Calendar.MONTH,omcLongRangeServiceOrder.getNumbers());
                    long expirationTime = enterTime.getTimeInMillis();

                    //当前时间
                    Calendar theDate = Calendar.getInstance();
                    theDate.setTime(new Date());
                    long theDateTimeInMillis = theDate.getTimeInMillis();

                    //写入过期状态
                    if (expirationTime<theDateTimeInMillis){
                        omcLongRangeServiceOrder.setOrderStatus(LongRangeServiceOrderStatusConstant.ALREADYEXPIRED);
                        System.out.println("过期,更改状态");
                        longRangeServiceOrderService.update(omcLongRangeServiceOrder);
                    }
                }
                //如果显示次数已经用完,计算是否要新的自然月重新计算

            }
        }
    }
}
